package com.example.myapplication.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.example.myapplication.ui.fragment.SettingFragment
import com.xge.component_caipu.CaiPuFragment
import com.xge.component_caipu.CollectFragment

class MainAdapter (
    fm: FragmentManager,
) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    override fun getCount(): Int {
        return 3 //此处返回的数量为页数总和(从1开始)
    }

    override fun getItem(position: Int): Fragment {
        return when (position) {
            TYPE_1 -> CaiPuFragment.newInstance()
            TYPE_2 -> CollectFragment.newInstance()
            TYPE_3 -> SettingFragment.newInstance()
            else -> throw IllegalArgumentException("无效参数 $position")
        }
    }

    override fun getItemPosition(any: Any): Int {
        return when (any) {
            is CaiPuFragment -> TYPE_1
            is CollectFragment -> TYPE_2
            is SettingFragment -> TYPE_3
            else -> super.getItemPosition(any)

        }
    }

    companion object {
        const val TYPE_1 = 0
        const val TYPE_2 = 1
        const val TYPE_3 = 2
    }
}
